//
// Surface.cpp for 207correl in /home/druart_p/Dropbox/Work/Teck2/math/my-math-epi/207
//
// Made by pierrick druart
// Login   <druart_p@epitech.net>
//
// Started on  Wed May 16 11:30:03 2012 pierrick druart
// Last update Wed May 23 17:17:22 2012 pierrick druart
//

#include	<sstream>

#include	"Surface.hh"
#include	"Calc.hh"

/******************************/
/* Constructors & Destructors */
/******************************/

Surface::Surface(char **av, std::list<int> girl, std::list<int> boy)
{
  QPalette		palette(Surface::palette());

  this->length = LENGTH;
  this->heigth = HEIGTH;
  this->av = av;
  this->_boy = boy;
  this->_girl = girl;
  setWindowTitle("Math 207correl");
  setFixedSize(this->length, this->heigth);
  palette.setColor(backgroundRole(), Qt::black);
  setPalette(palette);
  limite();
}

Surface::~Surface() {}

/*********************/
/* Limite repere     */
/*********************/

void	Surface::limite()
{
  std::list<int>::iterator	it;
  std::list<int>::iterator	it2;
  bool				first = true;

  it2 = this->_girl.begin();
  for (it = this->_boy.begin(); it != this->_boy.end(); ++it)
    {
      if (first == true)
	{
	  this->max_girl = *it2;
	  this->max_boy = *it;
	  this->min_boy = *it;
	  this->min_girl = *it2;
  	  first = false;
	}
      if (this->max_girl < *it2)
	this->max_girl = *it2;
      if (this->max_boy < *it)
	this->max_boy = *it;
      if (this->min_boy > *it)
	this->min_boy = *it;
      if (this->min_girl > *it2)
	this->min_girl = *it2;
      ++it2;
    }
  this->max_girl += 1;
  this->max_boy += 1;
  this->min_boy -= 1;
  this->min_girl -= 1;
  this->rap_y = this->heigth / (this->max_boy - this->min_boy);
  this->rap_x = this->length / (this->max_girl - this->min_girl);
}

/*********************/
/*  Painter Event    */
/*********************/

void		Surface::paintEvent(QPaintEvent *)
{
  QPainter		painter(this);
  int			i;
  int			j;
  int			trait = this->rap_x - 50;
  int			trait2 = this->rap_y - 50;

  painter.setPen(Qt::white);
  for (i = 0; i < this->length; i++)
    for (j = this->heigth; j > 0; j--)
      {
	if (i == 50 || j == (this->heigth - 50))
	  {
	    trait++;
	    trait2++;
	    painter.drawPoint(i, j);
	    if (trait == this->rap_x && j == (this->heigth - 50))
	      {
		painter.drawLine(i + this->rap_x, j - 3, i + this->rap_x, j + 3);
		trait = 0;
	      }
	    else if (trait == this->rap_x)
	      trait = 0;
	    if (trait2 == this->rap_y && i == 50)
	      {
		painter.drawLine(i - 3, j + this->rap_y, i + 3, j + this->rap_y);
		trait2 = 0;
	      }
	    else if (trait2 == this->rap_y)
	      trait2 = 0;
	  }
      }
  point(&painter);
}

// TRACER LINE

void		Surface::point(QPainter *painter)
{
  std::list<int>::iterator	it;
  std::list<int>::iterator	it2;
  int				x = 0;
  int				y = 0;

  painter->setBrush(Qt::red);
  painter->setPen(Qt::green);
  it2 = this->_girl.begin();
  for (it = this->_boy.begin(); it != this->_boy.end(); ++it)
    {
      x = (this->rap_x * (*it2 - this->min_girl)) + 50;
      y = this->heigth - ((this->rap_y * (*it - this->min_boy)) + 50);
      painter->drawRect(x, y, 3, 3);
      ++it2;
    }
  line(painter);
}

void		Surface::line(QPainter *painter)
{
  Calc	       	coef;
  coef.coefx_to_y(this->_boy, this->_girl);
  coef.coefy_to_x(this->_boy, this->_girl);
  double	a = coef.getAdeX();
  double       	b = coef.getBdeX();
  double	x1;
  double	x2;
  double	y1;
  double	y2;

  // X TO Y
  x1 = this->min_boy;
  y1 = a + b * x1;
  x2 = this->max_boy;
  y2 = a + b * x2;
  x1 = (this->rap_x * (x1 - this->min_girl)) + 50;
  y1 = this->heigth - ((this->rap_y * (y1 - this->min_boy)) + 50);
  x2 = (this->rap_x * (x2 - this->min_girl)) + 50;
  y2 = this->heigth - ((this->rap_y * (y2 - this->min_boy)) + 50);
  painter->setPen(Qt::darkCyan);
  painter->drawLine(x1, y1, x2, y2);
  painter->drawText(600, 50, "Y=(X)");

  // Y TO X
  a = coef.getAdeY();
  b = coef.getBdeY();
  y1 = this->min_girl;
  x1 = a + b * y1;
  y2 = this->max_girl;
  x2 = a + b * y2;
  x1 = (this->rap_x * (x1 - this->min_girl)) + 50;
  y1 = this->heigth - ((this->rap_y * (y1 - this->min_boy)) + 50);
  x2 = (this->rap_x * (x2 - this->min_girl)) + 50;
  y2 = this->heigth - ((this->rap_y * (y2 - this->min_boy)) + 50);
  painter->setPen(Qt::magenta);
  painter->drawLine(x1, y1, x2, y2);
  painter->drawText(600, 25, "X=(Y)");
}
